package com.kaoshi.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaoshi.common.config.Result;
import com.kaoshi.entity.BX;
import com.kaoshi.entity.Bao;
import com.kaoshi.entity.Xiangmu;
import com.kaoshi.mapper.XiangmuMapper;
import com.kaoshi.service.IBXService;
import com.kaoshi.service.IBaoService;
import com.kaoshi.service.IXiangmuService;
import com.mysql.cj.util.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.Objects;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-29
 */
@RestController
@CrossOrigin
@RequestMapping("/bao")
public class BaoController {
    @Autowired
    private IBaoService baoService;

    @Autowired
    private IBXService bxService;

    @Autowired
    private IXiangmuService xiangmuService;

    @RequestMapping("getAll")
    public Result getAll(){
        return Result.ok(baoService.list());
    }

    @RequestMapping("getAll1")
    public Result getAll1(@RequestBody Bao bao){
        LambdaQueryWrapper<Bao> qw = new LambdaQueryWrapper<>();
        qw.eq(Bao::getDel, 1).eq(Bao::getLx, 0);
        if (bao.getName()!=null&& bao.getName()!=""){
            qw.like(Bao::getName,bao.getName());
        }
        if (bao.getZt()!=null){
            qw.eq(Bao::getZt,bao.getZt());
        }
        return Result.ok(baoService.list(qw));
    }
    @RequestMapping("getAll2")
    public Result getAll2(@RequestBody Bao bao){
        LambdaQueryWrapper<Bao> qw = new LambdaQueryWrapper<>();
        qw.eq(Bao::getDel, 1).eq(Bao::getLx, 1);
        if (bao.getName()!=null&& bao.getName()!=""){
            qw.like(Bao::getName,bao.getName());
        }
        if (bao.getZt()!=null){
            qw.eq(Bao::getZt,bao.getZt());
        }
        return Result.ok(baoService.list(qw));
    }
    @RequestMapping("getAll3")
    public Result getAll3(@RequestBody Bao bao){
        LambdaQueryWrapper<Bao> qw = new LambdaQueryWrapper<>();
        qw.eq(Bao::getDel, 1).eq(Bao::getLx, 2);
        if (bao.getName()!=null&& bao.getName()!=""){
            qw.like(Bao::getName,bao.getName());
        }
        if (bao.getZt()!=null){
            qw.eq(Bao::getZt,bao.getZt());
        }
        return Result.ok(baoService.list(qw));
    }

    @Value("${aliyun.endpoint}")
    private String endpoint;
    @Value("${aliyun.keyId}")
    private String keyid;
    @Value("${aliyun.keySecret}")
    private String keysecret;
    @Value("${aliyun.bucketName}")
    private String buncket;
    @RequestMapping("tp")
    public String fileUpload(MultipartFile file) {
        String url="";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, keyid, keysecret);
        try {
            // 创建PutObjectRequest对象。
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();

            String replace = UUID.randomUUID().toString().replace("-", "");
            originalFilename = replace+originalFilename;
            String time = new DateTime().toString("yyyy/MM/dd");
            originalFilename = time+"/"+originalFilename;

            PutObjectResult putObjectResult = ossClient.putObject(buncket, originalFilename, inputStream);

            url="https://"+buncket+"."+endpoint+"/"+originalFilename;
        } catch (Exception ce) {
            System.out.println("上传异常--");
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return url;
    }
    @RequestMapping("save")
    public Result save(Integer[] ids,@RequestBody Bao bao) {
        baoService.save(bao);
        Integer id = bao.getId();
        for (Integer i : ids) {
            BX bx = new BX();
            bx.setBid(id);
            bx.setXid(i);
            bxService.save(bx);
        }
        return Result.ok();
    }
    @RequestMapping("del")
    public Result del(Integer id,String token) {
        List<BX> list = bxService.list(new LambdaQueryWrapper<BX>().eq(BX::getBid, token));
        for (BX bx : list) {
            if (bx.getXid().equals(id)){
                bx.setDel(0);
                bxService.updateById(bx);
            }
        }
        return Result.ok();
    }




    @RequestMapping("/xm")
    public Result xm(Integer[] ids){
        LambdaQueryWrapper<Xiangmu> qw = new LambdaQueryWrapper<>();
        System.out.println(Arrays.asList(ids)+"---------------------------------------------------------");
        qw.in(Xiangmu::getId, Arrays.asList(ids));
        return Result.ok(xiangmuService.list(qw));
    }

    @RequestMapping("/getfw")
    public Result getfw(String token){
        if (token!=null && token!=""){
            List<BX> list = bxService.list(new LambdaQueryWrapper<BX>().eq(BX::getBid, token).eq(BX::getDel, 1));
            List<Integer> integers = new ArrayList<>();
            for (BX bx : list) {
                integers.add(bx.getXid());
            }
            return Result.ok(xiangmuService.list(new LambdaQueryWrapper<Xiangmu>().in(Xiangmu::getId, integers)));
        }
        List<Xiangmu> l = new ArrayList<>();
        return Result.ok(l);
    }
    @RequestMapping("findbao")
    public Result findone(Integer id){
        List<Bao> list = baoService.list(new LambdaQueryWrapper<Bao>().eq(Bao::getId, id));
        return Result.ok(list.get(0));
    }
    @RequestMapping("selbyxid")
    public Result selbyxid(Integer id){
        List<BX> list = bxService.list(new LambdaQueryWrapper<BX>().eq(BX::getBid, id));
        List<Integer> integers = new ArrayList<>();
        for (BX bx : list) {
            integers.add(bx.getXid());
        }
        List<Xiangmu> list1 = new ArrayList<>();
        for (Integer integer : integers) {
            LambdaQueryWrapper<Xiangmu> qw = new LambdaQueryWrapper<>();
            qw.eq(Xiangmu::getDel,1);
            qw.eq(Xiangmu::getId,integer);
            list1.addAll(xiangmuService.list(qw));
        }

        return Result.ok(list1);
    }

    @RequestMapping("getBao")
    public List<Bao> getBao(){
        return baoService.list();
    }
}
